package cn.wmw.xyy.web.controller;

import cn.wmw.xyy.dto.CourseDto;
import cn.wmw.xyy.service.ICourseService;
import cn.wmw.xyy.domain.Course;
import cn.wmw.xyy.query.CourseQuery;
import cn.wmw.result.JsonResult;
import cn.wmw.result.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult save(@RequestBody @Valid CourseDto courseDto){
//        if(course.getId()!=null){
//            courseService.updateById(course);
//        }else{
//            courseService.insert(course);
//        }
        courseService.saveAll(courseDto);
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
//        Wrapper<Course> wrapper = new EntityWrapper<>();
//        wrapper.like("name", query.getKeyword());
        page = courseService.selectPage(page, new EntityWrapper<Course>().like("name", query.getKeyword()));
        return JsonResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
    /**
     * 课程上架
     * @param courseId
     * @return
     */
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    public JsonResult onLineCourse(@PathVariable("courseId") Long courseId){
        courseService.onLineCourse(courseId);
        return JsonResult.success();
    }

    /**
     * 根据课程Id查询课程详情
     * @return
     */
    @RequestMapping(value = "/detail/data/{courseId}",method = RequestMethod.GET)
    public JsonResult detailData(@PathVariable("courseId") Long id){
        return JsonResult.success(courseService.detailData(id));
    }

    /**
     * 根据课程订单们查询课程信息
     */
    @RequestMapping(value = "/info/{courseId}",method = RequestMethod.GET)
    public JsonResult info(@PathVariable("courseId")String courseId){
        return JsonResult.success(courseService.info(courseId));
    }
}
